﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace ManageStoreHUHTAMAKI
{
    public partial class frmManageStore : Form
    {
        public frmManageStore()
        {
            InitializeComponent();
            
        }

        private void ManageStore_Load(object sender, EventArgs e)
        {
           //panelDangNhap.Visible = true;
            //panelNhapTuNhaCC.Visible = false;
           
        }

        private void dangNhapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DangNhap();
        }

        public void DangNhap()
        {
            DangNhap _dangNhap = new DangNhap();
           // _dangNhap.MdiParent = this;
            frmManageStore _manageStore = new frmManageStore();
            _dangNhap.ShowDialog();
            if (!string.IsNullOrEmpty(_dangNhap._username) && !string.IsNullOrEmpty(_dangNhap._pass))
            {
                nhapKhoToolStripMenuItem.Enabled = true;
                xuatNguyenLieuToolStripMenuItem.Enabled = true;
                traNguyenLieuToolStripMenuItem.Enabled = true;
                themVatTuToolStripMenuItem.Enabled = true;
            }
        }

        public void DangXuat()
        {
            nhapKhoToolStripMenuItem.Enabled = false;
            xuatNguyenLieuToolStripMenuItem.Enabled = false;
            traNguyenLieuToolStripMenuItem.Enabled = false;
            themVatTuToolStripMenuItem.Enabled = false;
        }

        private void dangXuatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DangXuat();
        }

        private void toolStripNhapNguyenLieuTuNCC_Click(object sender, EventArgs e)
        {
           // panelDangNhap.Visible = false;
            panelNhapTuNhaCC.Visible = true;

            var NCC = new NhaCungCapDAO();
            var listNCC = NCC.GetTenNhaCungCap ();
            if (listNCC != null && listNCC.Count > 0)
            {
                comboBoxNcc.DataSource = null;
                comboBoxNcc.DataSource = listNCC;
            }

            var Kho = new KhoDAO();
            var listKho = Kho.GetTenKho();
            if (listKho != null && listKho.Count > 0)
            {
                comboBoxKho.DataSource = null;
                comboBoxKho.DataSource = listKho;
            }

            var DVT = new DonViTinhDAO();
            var listDVT = DVT.GetDVT();
            var VatTu = new VatTuDAO();
            var listVatTu = VatTu.GetVatTu();
            dataGridViewNhapTuNCC.Columns.Add("maVatTu", "Mã vật tư");
            if (listVatTu != null)
            {
                DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
                cmb.HeaderText = "Tên vật tư";
                cmb.Name = "TenVatTu";
                cmb.DataSource = listVatTu;
                cmb.DisplayMember = "tenVT";
                cmb.ValueMember = "maVT";
                cmb.Width = 250;
                dataGridViewNhapTuNCC.Columns.Add(cmb);
            }

            dataGridViewNhapTuNCC.Columns.Add("DVT", "Đơn vị tính");
            dataGridViewNhapTuNCC.Columns.Add("soLuongNhap", "Số lượng nhập");
            dataGridViewNhapTuNCC.Columns.Add("soLuongThuc", "Số lượng thực");
            dataGridViewNhapTuNCC.Columns.Add("ghichu", "Ghi chú");

            dataGridViewNhapTuNCC.Columns[0].Width = 100;
            dataGridViewNhapTuNCC.Columns[3].Width = 100;
            dataGridViewNhapTuNCC.Columns[4].Width = 100;
            dataGridViewNhapTuNCC.Columns[5].Width = 250;
        }

        private void thêmVậtTưToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var _vatTu = new VatTu();
            _vatTu.ShowDialog();
        }

        private void dataGridViewNhapTuNCC_SelectionChanged(object sender, EventArgs e)
        {
            var x = sender as ct_giaodich;
            
        }

        private void dataGridViewNhapTuNCC_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                cb.SelectedValueChanged +=new EventHandler(cb_SelectedValueChanged); 
            }
        }

        void cb_SelectedValueChanged(object sender, EventArgs e)
        {
            var x = sender as System.Windows.Forms.DataGridViewComboBoxEditingControl ;
            if (x != null)
            {
                var y = x.EditingControlFormattedValue as string;
                if (!string.IsNullOrEmpty(y))
                {
                    var VatTu = new VatTuDAO();
                    var listVatTu = VatTu.GetVatTu();
                    var DVT = new DonViTinhDAO();
                    var listDVT = DVT.GetDVT();
                    foreach (var item in listVatTu)
                    {
                        if (item.tenVT.Trim().ToLower() == y.Trim().ToLower())
                        {
                            int dvt = item.maDVT.Value;
                            int mavt = item.maVT;
                            foreach (var _dvt in listDVT)
                            {
                                if (dvt == _dvt.maDVT)
                                {
                                    string tendvt = _dvt.tenDVT;

                                    int rowSelected = dataGridViewNhapTuNCC.CurrentRow.Index;
                                    dataGridViewNhapTuNCC.Rows[rowSelected].Cells[0].Value = mavt;
                                    dataGridViewNhapTuNCC.Rows[rowSelected].Cells[2].Value = tendvt;

                                    break;
                                }
                            }
                            break;
                        }
                        
                    }
                }
            }
        }


        

       

    }
}
